package com.fourpigeon.domain;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fourpigeon.utils.COSUtils;
import com.fourpigeon.utils.LocalDateTimeSerializerUtils;
import com.qcloud.cos.http.HttpMethodName;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 含所有信息的博客类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BlogDaily{
    /**
     * 博客id
     */
    private Long id;
    /**
     * 用户id
     */
    private Long userId;
    /**
     * 赞
     */
    private Integer likes = 0;
    /**
     * 是否点赞
     */
    private boolean isLike = false;
    /**
     * 用户图标
     */
    private String icon;
    /**
     * 用户昵称
     */
    private String userName;
    /**
     * 评论数量
     */
    private Integer comments = 0;
    /**
     * 博客类型：0：日常，1：技术，2：吐槽
     */
    private Short type;
    /**
     * 博客内容
     */
    private String content;
    /**
     * 照片，上限9张
     */
    private String images;
    /**
     * 状态码：0正常，1：被举报，2：禁止查看
     */
    private Integer status = 0;
    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeSerializerUtils.LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializerUtils.LocalDateTimeSerializer.class)
    private LocalDateTime createTime;
    /**
     * 更新时间
     */
    @JsonDeserialize(using = LocalDateTimeSerializerUtils.LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializerUtils.LocalDateTimeSerializer.class)
    private LocalDateTime updateTime;

    /**
     * 头像url
     */
    private String iconUrl = "";

    /**
     * 图片url
     */
    private String imgsUrl = "";

    /**
     * 第一次创建时设置属性
     * @param user 用户对象
     */
    public void set(Long id,User user,Short blogType){
        this.id = id;
        this.type = blogType;
        this.userId = user.getId();
        this.userName = user.getName();
        this.icon = user.getIcon();
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
    }

    public void update(BlogDailyDTO b) {
        this.content = b.getContent();
        this.images = b.getImages();
    }

    public boolean isMatch(BlogDailyDTO b){
        if (!this.content.matches(b.getContent())) {
            return false;
        }
        return this.images.matches(b.getImages());
    }

    public void handlerImgs() {
        if (!StrUtil.isBlank(this.images)) {
            List<String> imgsList = StrUtil.split(images, ',');
            StringBuilder tmpImages = new StringBuilder();
            for (int i = 0;i<imgsList.size();i++) {
                String s = imgsList.get(i);
                String tmpUrlSuffix = COSUtils.getSign(s, HttpMethodName.GET);
                if (i>0) tmpImages.append(',');
                tmpImages.append(tmpUrlSuffix);
            }
            this.imgsUrl = tmpImages.toString();
        }
        this.iconUrl = COSUtils.getSign(this.icon,HttpMethodName.GET);
    }
}
